package com.dnzx.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.dnzx.framework.page.PageResult;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IOrderDao;
import com.dnzx.model.CartBO;
import com.dnzx.model.OrderBO;
import com.dnzx.model.OrderItemBO;
import com.dnzx.service.ICartService;
import com.dnzx.service.IOrderItemService;
import com.dnzx.service.IOrderService;

/**
 * 订单
 */
@Service
public class OrderServiceImpl implements IOrderService {

    @Resource
    private IOrderDao orderDao;
    @Resource
    private ICartService cartService;
    @Resource
	private IOrderItemService orderItemService;

    @Override
    public void save(OrderBO orderBO) throws Exception {
    	String sn = new Date().getTime() + "";
    	double amt = 0;
    	String picUrl = null;
    	List<Long> itemList = orderBO.getItemList();
    	for (int i = 0;i < itemList.size(); i++) {
    		Long cartId = itemList.get(i);
    		CartBO cartBO = this.cartService.get(cartId);
    		if (picUrl == null) {
    			picUrl = cartBO.getPicUrl();
    		}
    		// 保存订单明细信息
    		OrderItemBO orderItemBO = new OrderItemBO();
    		orderItemBO.setAddTime(new Date());
    		orderItemBO.setAmt(cartBO.getAmt());
    		orderItemBO.setCameraFlag(cartBO.getCameraFlag());
    		orderItemBO.setCartTime(cartBO.getCartTime());
    		orderItemBO.setCategoryId(cartBO.getCategoryId());
    		orderItemBO.setCeilingFlag(cartBO.getCeilingFlag());
    		orderItemBO.setColorValue(cartBO.getColorValue());
    		orderItemBO.setDeviceColor(cartBO.getDeviceColor());
    		orderItemBO.setCabinetColor(cartBO.getCabinetColor());
    		orderItemBO.setCabinetColorValue(cartBO.getCabinetColorValue());
    		orderItemBO.setDoorTotalCount(cartBO.getDoorTotalCount());
    		orderItemBO.setFootingFlag(cartBO.getFootingFlag());
    		orderItemBO.setOperationCode(cartBO.getOperationCode());
    		orderItemBO.setSn(sn);
    		orderItemBO.setPaymentTypeDevice(cartBO.getPaymentTypeDevice());
    		orderItemBO.setServerFlag(cartBO.getServerFlag());
    		orderItemBO.setSubCategoryCode(cartBO.getSubCategoryCode());
    		orderItemBO.setSubCategoryCount(cartBO.getSubCategoryCount());
    		orderItemBO.setSubCategoryTotalCount(cartBO.getSubCategoryTotalCount());
    		orderItemBO.setUserId(cartBO.getUserId());
    		orderItemBO.setWoodFlag(cartBO.getWoodFlag());
    		this.orderItemService.save(orderItemBO);
    		
    		amt = amt + cartBO.getAmt();
    		this.cartService.delete(cartId);
    	}
    	orderBO.setPicUrl(picUrl);
    	orderBO.setSn(sn);
    	orderBO.setAmt(amt);
    	orderBO.setStatus("1");
    	this.orderDao.save(orderBO);
    }

    @Override
    public void delete(Long id) throws Exception {
        this.orderDao.delete(id);
    }

    @Override
    public void update(OrderBO orderBO) throws Exception {
        this.orderDao.update(orderBO);
    }

    @Override
    public OrderBO get(Long id) {
    	OrderBO orderBO = this.orderDao.get(id);
    	if (orderBO != null) {
    		OrderItemBO OrderItemBOQuery = new OrderItemBO();
    		OrderItemBOQuery.setSn(orderBO.getSn());
    		List<OrderItemBO> orderItemBOList = this.orderItemService.queryList(OrderItemBOQuery);
    		orderBO.setOrderItemBOList(orderItemBOList);
    		return orderBO;
    	}
        return null;
    }
    
    @Override
    public OrderBO get(OrderBO orderBO) {
    	List<OrderBO> orderBOList = this.orderDao.queryList(orderBO);
    	if (orderBOList != null && orderBOList.size() > 0) {
    		orderBO = orderBOList.get(0);
    		OrderItemBO OrderItemBOQuery = new OrderItemBO();
    		OrderItemBOQuery.setSn(orderBO.getSn());
    		List<OrderItemBO> orderItemBOList = this.orderItemService.queryList(OrderItemBOQuery);
    		orderBO.setOrderItemBOList(orderItemBOList);
    		return orderBO;
    	}
        return null;
    }
    
    @Override
    public List<OrderBO> queryList(OrderBO orderBO) {
        return this.orderDao.queryList(orderBO);
    }
    
    @Override
    public PageResult<List<OrderBO>> queryPage(OrderBO orderBO, PageResult<List<OrderBO>> pageResult) {
        int totalRows = this.orderDao.queryCount(orderBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(orderBO.getCurrentPage());
        pageResult.setPageSize(orderBO.getPageSize());
		List<OrderBO> records = this.orderDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), orderBO);
		pageResult.setT(records);
		return pageResult;
    }

}
